using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using NetProtocol;
using NetProtocol.Client;
using NetProtocol.Enum;
using NetProtocol.POD;

using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class LogicServerTestModule : Singleton<LogicServerTestModule>, CNetLogicServerTest
    {
        public static CNetLogicServerTest NetCore => Instance;

        private LogicServerTestModule()
        {
            //TestUpload().Forget();
            //TestSend().Forget();
            //TestNetSend().Forget();
        }

        private int test_counter = 0;
        private async UniTaskVoid TestSend()
        {
            await UniTask.Delay(3000);
            while (true)
            {
                await UniTask.Delay(100);
                if (LogicServerModule.Instance.CurrSession == null)
                {
                    continue;
                }
                if (Random.Range(0, 1f) < 0)
                {
                    if (LogicServerModule.Instance.CurrSession.IsAvailable)
                    {
                        LogicServerModule.Instance.CurrSession.Disconnect();
                    }
                }
                var result = await NetCore.CS_testCount(++test_counter);
                if (result.IsSuccess)
                {
                    if (result.count != test_counter)
                    {
                        throw new System.Exception($"CS_testCount error, now counter = {test_counter}, result.count = {result.count}");
                    }
                }
                if (Random.Range(0, 1f) < 0)
                {
                    if (LogicServerModule.Instance.CurrSession.IsAvailable)
                    {
                        LogicServerModule.Instance.CurrSession.Disconnect();
                    }
                }
            }
        }

        private int upload_counter = 0;

        private async UniTaskVoid TestUpload()
        {
            await UniTask.Delay(3000);
            while (true)
            {
                await UniTask.Delay(100);
                if (LogicServerModule.Instance.CurrSession == null)
                {
                    continue;
                }
                NetCore.CS_uploadCount(++upload_counter);
            }
        }

        private int notify_counter;
        void CNetLogicServerTest.SC_notifyCount(ISession session, int count)
        {
            if (++notify_counter != count)
            {
                throw new System.Exception($"SC_notifyCount error, now counter = {notify_counter}, count = {count}");
            }
        }


        public async UniTaskVoid TestNetSend()
        {
            await UniTask.Delay(6000);
            sbyte b0 = 123;
            byte[] a0 = new byte[] { 124, 234, 0, 99 };
            List<byte[]> a00 = new List<byte[]>();
            a00.Add(a0);
            Dictionary<int, byte[]> a000 = new Dictionary<int, byte[]>();
            a000.Add(1, a0);
            uint a1 = 3000000000;
            byte a2 = 245;
            ushort a3 = ushort.MaxValue;

            string s = "test";
            int ix = int.MaxValue;
            float f = float.MaxValue;
            double d = double.MaxValue;
            long l = long.MaxValue;
            bool bx = true;

            float f11 = f / 2;

            List<string> sList = null;// new List<string> { s, s };
            List<int> iList = new List<int> { ix, ix };
            List<float> fList = new List<float> { f, f };
            List<double> dList = new List<double> { d, d };
            List<long> lList = new List<long> { l, l };
            List<bool> bList = new List<bool> { bx, bx };
            Dictionary<int, string> isMap = null;//new Dictionary<int, string> { { ix, s }, { ix / 2, s } };
            Dictionary<int, int> iiMap = new Dictionary<int, int> { { ix, ix }, { ix / 2, ix } };
            Dictionary<int, float> ifMap = new Dictionary<int, float> { { ix, f }, { ix / 2, f } };
            Dictionary<int, double> idMap = new Dictionary<int, double> { { ix, d }, { ix / 2, d } };
            Dictionary<int, long> ilMap = new Dictionary<int, long> { { ix, l }, { ix / 2, l } };
            Dictionary<int, bool> ibMap = new Dictionary<int, bool> { { ix, bx }, { ix / 2, bx } };
            Dictionary<string, int> siMap = new Dictionary<string, int> { { s, ix }, { s + "1", ix } };
            Dictionary<float, int> fiMap = new Dictionary<float, int> { { f, ix }, { f / 2, ix } };
            Dictionary<double, int> diMap = new Dictionary<double, int> { { d, ix }, { d / 2, ix } };
            Dictionary<long, int> liMap = new Dictionary<long, int> { { l, ix }, { l / 2, ix } };
            Dictionary<bool, int> biMap = new Dictionary<bool, int> { { true, ix }, { false, ix } };

            TestPOD2 testPOD2 = new TestPOD2();
            testPOD2.bx = false;
            testPOD2.d = double.MinValue;
            //testPOD2.s = "TestPOD2";
            testPOD2.l = long.MinValue;
            testPOD2.f = float.MinValue;
            testPOD2.testEnum = TestEnum.b;
            //testPOD2.ix = int.MinValue;

            TestPOD1 testPOD1 = new TestPOD1();
            testPOD1.unsafe_sList = sList;
            testPOD1.unsafe_iList = iList;
            testPOD1.unsafe_fList = fList;
            testPOD1.unsafe_dList = dList;
            testPOD1.unsafe_lList = lList;
            testPOD1.unsafe_bList = bList;

            testPOD1.unsafe_isMap = isMap;
            testPOD1.unsafe_iiMap = iiMap;
            testPOD1.unsafe_ifMap = ifMap;
            testPOD1.unsafe_idMap = idMap;
            testPOD1.unsafe_ilMap = ilMap;
            testPOD1.unsafe_ibMap = ibMap;

            testPOD1.unsafe_siMap = siMap;
            testPOD1.unsafe_fiMap = fiMap;
            testPOD1.unsafe_diMap = diMap;
            testPOD1.unsafe_liMap = liMap;
            testPOD1.unsafe_biMap = biMap;

            testPOD1.pod2 = testPOD2;
            testPOD1.unsafe_pod2List = null;//new List<TestPOD2> { testPOD2 };
            testPOD1.unsafe_pod2iMap = null;//new Dictionary<TestPOD2, int> { { testPOD2, 1 } };
            testPOD1.unsafe_ipod2Map = null;//new Dictionary<int, TestPOD2> { { 1, testPOD2 } };

            TestPOD pod = new TestPOD();
            pod.pod1 = testPOD1;
            pod.unsafe_pod1List = new List<TestPOD1> { testPOD1 };
            pod.unsafe_pod1iMap = new Dictionary<TestPOD1, int> { { testPOD1, 1 } };
            pod.unsafe_ipod1Map = new Dictionary<int, TestPOD1> { { 1, testPOD1 } };

            List<TestPOD> podList = new List<TestPOD> { pod };
            Dictionary<int, TestPOD> ipodMap = new Dictionary<int, TestPOD> { { 1, pod } };
            Dictionary<TestPOD, int> podiMap = new Dictionary<TestPOD, int> { { pod, 2 } };

            var result = await NetCore.CS_test(TestEnum.a, b0, a0, a00, a000, a1, a2, a3, s, ix, f, d, l, bx, pod, sList, iList, fList, dList, lList, bList, isMap, iiMap, ifMap, idMap, ilMap, ibMap, siMap, fiMap, diMap, liMap, biMap, podList, ipodMap, podiMap);
            LogGame.Log(result.code);
        }

        ISession CNetLogicServerTest.GetSession()
        {
            return LogicServerModule.Instance.CurrSession;
        }
    }

}
